<?php 
class ControllerCommunityForumSetting extends Controller {
	private $error = array();
 
	public function index() {
        $this->load->language('community/forum_setting');

        $this->document->setTitle($this->language->get('heading_title'));

        $this->load->model('community/forum_setting');

        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
            $allowTypes=rtrim($this->request->post['allowtypes'],',');
            if(empty($allowTypes)){
                $this->request->post['allowtypes']=array();
            }else{
                $this->request->post['allowtypes']=explode(',',$allowTypes);
            }

            $disallowUsername=rtrim($this->request->post['disallow_username'],',');
            if(empty($disallowUsername)){
                $this->request->post['disallow_username']=array();
            }else{
                $this->request->post['disallow_username']=explode(',',$disallowUsername);
            }

            if(!isset($this->request->post['cart_topic_allowed_forums'])){
                $this->request->post['cart_topic_allowed_forums']=array();
            }

            $this->request->post['topics_be_read_time']=floor($this->request->post['topics_be_read_time']*60*60*24);

            $this->request->post['poll_ip_vote_time_limit']=floor($this->request->post['poll_ip_vote_time_limit']*60*60);

            $this->request->post['warning_decrement_time_span']=floor($this->request->post['warning_decrement_time_span']*60*60*24);

            $this->request->post['warning_ban_time_span']=floor($this->request->post['warning_ban_time_span']*60*60*24);

            $this->model_community_forum_setting->editForumSetting('community_config', $this->request->post);

            $this->session->data['success'] = $this->language->get('text_success');

            $this->redirect($this->url->link('community/forum', 'token=' . $this->session->data['token'], 'SSL'));
        }

        $this->data['heading_title'] = $this->language->get('heading_title');

        $this->data['text_none'] = $this->language->get('text_none');
        $this->data['text_enabled'] = $this->language->get('text_enabled');
        $this->data['text_disabled'] = $this->language->get('text_disabled');
        $this->data['text_select_all'] = $this->language->get('text_select_all');
        $this->data['text_unselect_all'] = $this->language->get('text_unselect_all');

        $this->data['entry_avatar_max_size'] = $this->language->get('entry_avatar_max_size');
        $this->data['entry_allowed_avatar_type'] = $this->language->get('entry_allowed_avatar_type');
        $this->data['entry_avatar_max_width'] = $this->language->get('entry_avatar_max_width');
        $this->data['entry_avatar_max_height'] = $this->language->get('entry_avatar_max_height');
        $this->data['entry_disallowed_user_name'] = $this->language->get('entry_disallowed_user_name');
        $this->data['entry_cart_forums'] = $this->language->get('entry_cart_forums');
        $this->data['entry_posting_max_subject_length'] = $this->language->get('entry_posting_max_subject_length');
        $this->data['entry_posting_max_content_length'] = $this->language->get('entry_posting_max_content_length');
        $this->data['entry_post_num_per_page'] = $this->language->get('entry_post_num_per_page');
        $this->data['entry_topic_num_per_page'] = $this->language->get('entry_topic_num_per_page');
        $this->data['entry_daily_awarded_posting_count'] = $this->language->get('entry_daily_awarded_posting_count');
        $this->data['entry_posting_awarded_forum_points'] = $this->language->get('entry_posting_awarded_forum_points');
        $this->data['entry_topic_be_read_time'] = $this->language->get('entry_topic_be_read_time');
        $this->data['entry_bbcode_parsing_status'] = $this->language->get('entry_bbcode_parsing_status');
        $this->data['entry_bbcode_img_parsing_status'] = $this->language->get('entry_bbcode_img_parsing_status');
        $this->data['entry_bbcode_flash_parsing_status'] = $this->language->get('entry_bbcode_flash_parsing_status');
        $this->data['entry_bbcode_url_parsing_status'] = $this->language->get('entry_bbcode_url_parsing_status');
        $this->data['entry_smiles_parsing_status'] = $this->language->get('entry_smiles_parsing_status');
        $this->data['entry_auto_url_parsing_status'] = $this->language->get('entry_auto_url_parsing_status');
        $this->data['entry_inbox_max_pm'] = $this->language->get('entry_inbox_max_pm');
        $this->data['entry_sent_message_max_pm'] = $this->language->get('entry_sent_message_max_pm');
        $this->data['entry_flood_limit'] = $this->language->get('entry_flood_limit');
        $this->data['entry_poll_ip_vote_time_limit'] = $this->language->get('entry_poll_ip_vote_time_limit');
        $this->data['entry_online_time_span'] = $this->language->get('entry_online_time_span');
        $this->data['entry_warning_decrement_time_span'] = $this->language->get('entry_warning_decrement_time_span');
        $this->data['entry_warning_ban_count'] = $this->language->get('entry_warning_ban_count');
        $this->data['entry_daily_bonus'] = $this->language->get('entry_daily_bonus');
        $this->data['entry_lottery_cost'] = $this->language->get('entry_lottery_cost');
        $this->data['entry_best_answer_awarded_forum_points'] = $this->language->get('entry_best_answer_awarded_forum_points');
        $this->data['entry_featured_topic_awarded_forum_points'] = $this->language->get('entry_featured_topic_awarded_forum_points');
        $this->data['entry_warning_ban_time_span'] = $this->language->get('entry_warning_ban_time_span');
        $this->data['entry_yourday_forum'] = $this->language->get('entry_yourday_forum');
        $this->data['entry_community_email'] = $this->language->get('entry_community_email');

        $this->data['button_save'] = $this->language->get('button_save');
        $this->data['button_cancel'] = $this->language->get('button_cancel');

        $this->data['tab_user'] = $this->language->get('tab_user');
        $this->data['tab_posting'] = $this->language->get('tab_posting');
        $this->data['tab_system'] = $this->language->get('tab_system');

        if (isset($this->error['warning'])) {
            $this->data['error_warning'] = $this->error['warning'];
        } else {
            $this->data['error_warning'] = '';
        }

        $this->data['breadcrumbs'] = array();

        $this->data['breadcrumbs'][] = array(
            'text'      => $this->language->get('text_home'),
            'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
            'separator' => false
        );

        $this->data['breadcrumbs'][] = array(
            'text'      => $this->language->get('heading_title'),
            'href'      => $this->url->link('community/forum_setting', 'token=' . $this->session->data['token'], 'SSL'),
            'separator' => ' :: '
        );

        $this->data['cancel'] = $this->url->link('community/forum', 'token=' . $this->session->data['token'], 'SSL');

        $this->data['token'] = $this->session->data['token'];

        $results = $this->model_community_forum_setting->getForums();

        $this->data['forums']=array();
        $this->data['sub_forums']=array();

        foreach($results as $result){
            $this->data['forums'][]=array(
                'forum_id'      =>      $result['forum_id'],
                'forum_name'    =>      $result['forum_name']
            );

            $sub_forum_results = $this->model_community_forum_setting->getSubForums($result['forum_id']);

            foreach($sub_forum_results as $sub_forum_result){
                $this->data['sub_forums'][$result['forum_id']][]=array(
                    'forum_id'      =>      $sub_forum_result['forum_id'],
                    'forum_name'    =>      '&nbsp;&nbsp;&nbsp;&nbsp;' . $sub_forum_result['forum_name']
                );
            }
        }

        $setting=$this->model_community_forum_setting->getForumSetting('community_config');

        $this->data['cart_allowed_forums']=$setting['cart_topic_allowed_forums'];

        if (isset($this->request->post['max_avatar_size'])) {
            $this->data['max_avatar_size'] = $this->request->post['max_avatar_size'];
        } else {
            $this->data['max_avatar_size'] = $setting['max_avatar_size'];
        }

        if (isset($this->request->post['allowtypes'])) {
            $this->data['allowtypes'] = $this->request->post['allowtypes'];
        } else {
            $allowTypes=$setting['allowtypes'];

            $allowTypeStr='';

            foreach($allowTypes as $allowType){
                $allowTypeStr.=$allowType . ',';
            }

            $allowTypeStr=rtrim($allowTypeStr,',');

            $this->data['allowtypes'] = $allowTypeStr;
        }

        if (isset($this->request->post['max_avatar_width'])) {
            $this->data['max_avatar_width'] = $this->request->post['max_avatar_width'];
        } else {
            $this->data['max_avatar_width'] = $setting['max_avatar_width'];
        }

        if (isset($this->request->post['max_avatar_height'])) {
            $this->data['max_avatar_height'] = $this->request->post['max_avatar_height'];
        } else {
            $this->data['max_avatar_height'] = $setting['max_avatar_height'];
        }

        if (isset($this->request->post['disallow_username'])) {
            $this->data['disallow_username'] = $this->request->post['disallow_username'];
        } else {
            $disallowedUserNames=$setting['disallow_username'];

            $disallowedUserNameStr='';

            foreach($disallowedUserNames as $disallowedUserName){
                $disallowedUserNameStr.=$disallowedUserName . ',';
            }

            $disallowedUserNameStr=rtrim($disallowedUserNameStr,',');

            $this->data['disallow_username'] = $disallowedUserNameStr;
        }

        if (isset($this->request->post['posting_max_subject_length'])) {
            $this->data['posting_max_subject_length'] = $this->request->post['posting_max_subject_length'];
        } else {
            $this->data['posting_max_subject_length'] = $setting['posting_max_subject_length'];
        }

        if (isset($this->request->post['posting_max_content_length'])) {
            $this->data['posting_max_content_length'] = $this->request->post['posting_max_content_length'];
        } else {
            $this->data['posting_max_content_length'] = $setting['posting_max_content_length'];
        }

        if (isset($this->request->post['topic_max_posts_per_page'])) {
            $this->data['topic_max_posts_per_page'] = $this->request->post['topic_max_posts_per_page'];
        } else {
            $this->data['topic_max_posts_per_page'] = $setting['topic_max_posts_per_page'];
        }

        if (isset($this->request->post['forum_max_topic_per_page'])) {
            $this->data['forum_max_topic_per_page'] = $this->request->post['forum_max_topic_per_page'];
        } else {
            $this->data['forum_max_topic_per_page'] = $setting['forum_max_topic_per_page'];
        }
        if (isset($this->request->post['daily_awarded_posting_count'])) {
            $this->data['daily_awarded_posting_count'] = $this->request->post['daily_awarded_posting_count'];
        } else {
            $this->data['daily_awarded_posting_count'] = $setting['daily_awarded_posting_count'];
        }

        if (isset($this->request->post['posting_awarded_forum_points'])) {
            $this->data['posting_awarded_forum_points'] = $this->request->post['posting_awarded_forum_points'];
        } else {
            $this->data['posting_awarded_forum_points'] = $setting['posting_awarded_forum_points'];
        }

        if (isset($this->request->post['bbcode_parsing_enable'])) {
            $this->data['bbcode_parsing_enable'] = $this->request->post['bbcode_parsing_enable'];
        } else {
            $this->data['bbcode_parsing_enable'] = $setting['bbcode_parsing_enable'];
        }

        if (isset($this->request->post['bbcode_img_parsing_enable'])) {
            $this->data['bbcode_img_parsing_enable'] = $this->request->post['bbcode_img_parsing_enable'];
        } else {
            $this->data['bbcode_img_parsing_enable'] = $setting['bbcode_img_parsing_enable'];
        }

        if (isset($this->request->post['bbcode_flash_parsing_enable'])) {
            $this->data['bbcode_flash_parsing_enable'] = $this->request->post['bbcode_flash_parsing_enable'];
        } else {
            $this->data['bbcode_flash_parsing_enable'] = $setting['bbcode_flash_parsing_enable'];
        }

        if (isset($this->request->post['bbcode_url_parsing_enable'])) {
            $this->data['bbcode_url_parsing_enable'] = $this->request->post['bbcode_url_parsing_enable'];
        } else {
            $this->data['bbcode_url_parsing_enable'] = $setting['bbcode_url_parsing_enable'];
        }

        if (isset($this->request->post['smiles_parsing_enable'])) {
            $this->data['smiles_parsing_enable'] = $this->request->post['smiles_parsing_enable'];
        } else {
            $this->data['smiles_parsing_enable'] = $setting['smiles_parsing_enable'];
        }

        if (isset($this->request->post['auto_url_parsing_enable'])) {
            $this->data['auto_url_parsing_enable'] = $this->request->post['auto_url_parsing_enable'];
        } else {
            $this->data['auto_url_parsing_enable'] = $setting['auto_url_parsing_enable'];
        }

        if (isset($this->request->post['inbox_max_pm'])) {
            $this->data['inbox_max_pm'] = $this->request->post['inbox_max_pm'];
        } else {
            $this->data['inbox_max_pm'] = $setting['inbox_max_pm'];
        }

        if (isset($this->request->post['sent_message_max_pm'])) {
            $this->data['sent_message_max_pm'] = $this->request->post['sent_message_max_pm'];
        } else {
            $this->data['sent_message_max_pm'] = $setting['sent_message_max_pm'];
        }

        if (isset($this->request->post['flood_limit'])) {
            $this->data['flood_limit'] = $this->request->post['flood_limit'];
        } else {
            $this->data['flood_limit'] = $setting['flood_limit'];
        }

        if (isset($this->request->post['topics_be_read_time'])) {
            $this->data['topics_be_read_time'] = $this->request->post['topics_be_read_time'];
        } else {
            $this->data['topics_be_read_time'] = floor($setting['topics_be_read_time']/(60*60*24));
        }

        if (isset($this->request->post['poll_ip_vote_time_limit'])) {
            $this->data['poll_ip_vote_time_limit'] = $this->request->post['poll_ip_vote_time_limit'];
        } else {
            $this->data['poll_ip_vote_time_limit'] = floor($setting['poll_ip_vote_time_limit']/(60*60));
        }

        if (isset($this->request->post['online_time_span'])) {
            $this->data['online_time_span'] = $this->request->post['online_time_span'];
        } else {
            $this->data['online_time_span'] = $setting['online_time_span'];
        }

        if (isset($this->request->post['warning_decrement_time_span'])) {
            $this->data['warning_decrement_time_span'] = $this->request->post['warning_decrement_time_span'];
        } else {
            $this->data['warning_decrement_time_span'] = floor($setting['warning_decrement_time_span']/(60*60*24));
        }

        if (isset($this->request->post['warning_ban_count'])) {
            $this->data['warning_ban_count'] = $this->request->post['warning_ban_count'];
        } else {
            $this->data['warning_ban_count'] = $setting['warning_ban_count'];
        }

        if (isset($this->request->post['warning_ban_time_span'])) {
            $this->data['warning_ban_time_span'] = $this->request->post['warning_ban_time_span'];
        } else {
            $this->data['warning_ban_time_span'] = floor($setting['warning_ban_time_span']/(60*60*24));
        }

        if (isset($this->request->post['daily_bonus'])) {
            $this->data['daily_bonus'] = $this->request->post['daily_bonus'];
        } else {
            $this->data['daily_bonus'] = $setting['daily_bonus'];
        }

        if (isset($this->request->post['lottery_cost'])) {
            $this->data['lottery_cost'] = $this->request->post['lottery_cost'];
        } else {
            $this->data['lottery_cost'] = $setting['lottery_cost'];
        }

        if (isset($this->request->post['best_answer_awarded_forum_points'])) {
            $this->data['best_answer_awarded_forum_points'] = $this->request->post['best_answer_awarded_forum_points'];
        } else {
            $this->data['best_answer_awarded_forum_points'] = $setting['best_answer_awarded_forum_points'];
        }

        if (isset($this->request->post['featured_topic_awarded_forum_points'])) {
            $this->data['featured_topic_awarded_forum_points'] = $this->request->post['featured_topic_awarded_forum_points'];
        } else {
            $this->data['featured_topic_awarded_forum_points'] = $setting['featured_topic_awarded_forum_points'];
        }

        if (isset($this->request->post['config_community_email'])) {
            $this->data['config_community_email'] = $this->request->post['config_community_email'];
        } else {
            $this->data['config_community_email'] = $setting['config_community_email'];
        }

        if (isset($this->request->post['your_day_forum_id'])) {
            $this->data['your_day_forum_id'] = $this->request->post['your_day_forum_id'];
        } else {
            $this->data['your_day_forum_id'] = $setting['your_day_forum_id'];
        }

        $this->data['your_day_forums']=$this->model_community_forum_setting->getYourDayForums();

        $this->template = 'community/forum_setting.tpl';
        $this->children = array(
            'common/header',
            'common/footer'
        );

        $this->response->setOutput($this->render());
	}

	private function validateForm() {
		if (!$this->user->hasPermission('modify', 'community/forum_setting')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}

		if ($this->error && !isset($this->error['warning'])) {
			$this->error['warning'] = $this->language->get('error_warning');
		}
					
		if (!$this->error) {
			return true;
		} else {
			return false;
		}
	}
}
?>